package org.sheep.sprite;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.Log;

public class LaneManager {
  //first dimension -> lanes
  //second dimension -> bullets
  private ArrayList<Bullet>[] friendlyBullets;
  
  //first dimension -> lanes
  //second dimension -> enemies
  private ArrayList<Bullet>[] enemies;

  private Building[][] buildings;

  private Paint p = new Paint();
  private int lanePositions[];
  private int width;
  private int height;
  
  @SuppressWarnings("unchecked")
  public LaneManager(int width, int height, int tilewidth, int xOffset) {
    this.width = width;
    this.height = height;

    friendlyBullets = new ArrayList[width];
    enemies = new ArrayList[width];

    for(int i = 0; i < width; i++){
      friendlyBullets[i] = new ArrayList<Bullet>();
      enemies[i] = new ArrayList<Bullet>();
    }
    
    buildings = new Building[width][height];
    
    lanePositions = new int[width];
    
    for(int x = 0; x < width; x++)
      lanePositions[x] = (xOffset + x * tilewidth) + tilewidth/2;
  }
  
  public void spawnBuilding(int lane, int row, Building b) {
    synchronized(buildings){
      buildings[lane][row] =  b;
    }
  }
  
  public void spawnBullet(int lane, Bullet b) {
    friendlyBullets[lane].add(b);
  }
  
  public void spawnEnemy(int lane, Bullet v) {
    enemies[lane].add(v);
  }
  
  public void draw(Canvas c) {
    Bitmap im;
    
    for(int x = 0; x < width; x++) {
      for(int y = 0; y < height; y++) {
        if(buildings[x][y] != null) {
          im = buildings[x][y].getImage();
          c.drawBitmap(im, lanePositions[x],
              buildings[x][y].getPosition(), p);
        }
      }
    }

    for(int lane = 0; lane < width; lane++){
      for(Bullet enemy : enemies[lane]){
        im = enemy.getImage();
        Log.i("LaneManager", "w: " + im + ", lane: " + lane + ", lanePos: " +
            lanePositions[lane]);
        c.drawBitmap(im, lanePositions[lane],
            enemy.getPosition(), p);
      }
    }

    for(int lane = 0; lane < width; lane++) {
      for(Bullet friendlyBullet : friendlyBullets[lane]) {
        im = friendlyBullet.getImage();
        c.drawBitmap(im, lanePositions[lane],
            friendlyBullet.getPosition(), p);
      }
    }
  }
  
  public void update() {
    synchronized(buildings){
      int furthestBuilding = 0;
      
      for(int lane = 0; lane < width; lane++){
        
        //updates enemies
        for(furthestBuilding = height-1; furthestBuilding >= 0
            && buildings[lane][furthestBuilding] == null; furthestBuilding--);
        for(Bullet enemy : enemies[lane]){
          if(furthestBuilding == -1)
            enemy.move();
          else if(enemy.inFrontOf(buildings[lane][furthestBuilding])) {
            enemy.attack(buildings[lane][furthestBuilding]);
            buildings[lane][furthestBuilding].attack(enemy);
          } else
            enemy.move(buildings[lane][furthestBuilding]);
        }
        
        //updates bullets that are shot from buildings
        for(Bullet friendlyBullet : friendlyBullets[lane]) {
          for(Bullet enemy : enemies[lane]){
            friendlyBullet.move(enemy);
            if(friendlyBullet.inFrontOf(enemy)) {
              friendlyBullet.attack(enemy);
              enemy.attack(friendlyBullet);
            }
          }
        }
      }
    }
  }
  
  public void moveLeft(int row){
    synchronized(buildings){
      Building b = buildings[0][row];
    
      for(int x = 0; x < width-1; x++){
        buildings[x][row] = buildings[x][row+1];
      }

      buildings[buildings.length-1][row] = b;
    }
  }
  
  public void moveRight(int row){
    synchronized(buildings){
      Building b = buildings[buildings.length-1][row];
    
      for(int x = 1; x < width; x++){
        buildings[x][row] = buildings[x][row-1];
      }

      buildings[0][row] = b;
    }
  }

  public void setSize(int width, int height, int viewWidth, int viewHeight,
      Context context) {
    for(int x = 0; x < width; x++) {
      for(int y = 0; y < height; y++) {
        if(buildings[x][y] != null) {
          buildings[x][y].setSize(width, height, viewWidth, viewHeight, context);
        }
      }
    }

    for(int lane = 0; lane < width; lane++){
      for(Bullet enemy : enemies[lane]){
        enemy.setSize(width, height, viewWidth, viewHeight, context);
      }
    }

    for(int lane = 0; lane < width; lane++) {
      for(Bullet friendlyBullet : friendlyBullets[lane]) {
        friendlyBullet.setSize(width, height, viewWidth, viewHeight, context);
      }
    }
  }
}
